<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
  <script>

    //实现instanceof; 一个构造函数的prototype是否在实例上

    function judePrototo(source, target) {

      //目标构造函数不是函数类型；就抛错
      if(typeof target !== "function") {
        throw new TypeError("不是函数")
      };

      //实例必须是对象或者函数
      if(source === null || (typeof source != "object" && typeof source != "function")) {
        return false;
      }
      
      let prototype = Object.getPrototypeOf(source);

      while(true) {
        if(prototype == null) {
          return false;
        }

        if(prototype == target.prototype) {
          return true;
        }

        prototype = Object.getPrototypeOf(prototype);
      }
    }

    console.log(judePrototo(new String("1212"), String));

  </script>

</body>
</html>